﻿using CommonUtility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using WPay.Infrastructure.Unity;

namespace WPay.Domain.Model.DTOs
{
    /// <summary>
    /// API交互数据输出模型
    /// JSON数据示例： {"Success":true,"ErrorCode":0,"Message":"获取数据成功","Data":{"Name":"小李","Key":"Value","PWD":"abcdefg","OrgID":"0000"}}
    /// </summary>
    public class RespDto
    {
        /// <summary>
        /// API交互数据输出模型
        /// </summary>
        /// <param name="_success">操作是否成功[true:成功、false:失败]</param>
        /// <param name="_errorCode">操作代码[0:正常、200:失败]</param>
        /// <param name="_message">提示信息</param>
        /// <param name="_datas">返回数据包</param>
        public RespDto(bool _success, int _errorCode, string _message, Hashtable _data = null)
        {
            Success = _success;
            ErrorCode = _errorCode;
            Message = _message;
        }
        public RespDto()
        {
            Success = true;
        }
        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Success { get; set; } = true;
        /// <summary>
        /// 错误代码，仅当Success=false时使用
        /// </summary>
        public int ErrorCode { get; set; } = 200;
        /// <summary>
        /// 提示信息
        /// </summary>
        public string Message { get; set; } = "操作成功";
        /// <summary>
        /// 返回数据包
        /// </summary>
        public object Data { get; set; }
    }
    public static class RespDtoExtensions
    { 
        public static RespDto RunWithTry(this RespDto jsonResultEntry, Action<RespDto> runMethod)
        {

            try
            {
                runMethod(jsonResultEntry);
                return jsonResultEntry;
            }
            catch (UNException cex)
            {
                jsonResultEntry.ErrorCode = -1;
                jsonResultEntry.Message = cex.Message;
                jsonResultEntry.Success = false;
                LoggerHelper.Log(cex.InnerException?.Message);
            }
            catch (Exception e)
            {
                jsonResultEntry.ErrorCode = -1;
                jsonResultEntry.Message = "系统错误,请联系管理员";
                jsonResultEntry.Success = false;
                LoggerHelper.Log(e.Message + e.InnerException?.Message);
            }
            return jsonResultEntry;
        }
    }




    public class AlipayRespDto
    {

        public AlipayRespDto()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        public dynamic response { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string sign { get; set; }
    }

    public static class AlipayRespDtoExtensions
    {
        public static AlipayRespDto RunWithTry(this AlipayRespDto jsonResultEntry, Action<AlipayRespDto> runMethod)
        {

            try
            {
                runMethod(jsonResultEntry);
                return jsonResultEntry;
            }
            catch (UNException cex)
            {
                jsonResultEntry.response = new { code = "40004", msg = "BIZ_ERROR", sub_code = "BIZ_ERROR", sub_msg = $"业务异常--{cex.InnerException?.Message}" };
                LoggerHelper.Log(cex.InnerException?.Message);
            }
            catch (Exception e)
            {
                jsonResultEntry.response = new { code = "40004", msg = "SYSTEM_ERROR", sub_code = "SYSTEM_ERROR", sub_msg = $"系统繁忙--{e.InnerException?.Message}" };

                LoggerHelper.Log(e.Message + e.InnerException?.Message);
            }
            return jsonResultEntry;
        }
    }




}